package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {
    @DubboReference
    private TypeTemplateService typeTemplateService;


    @RequestMapping("toTypeTemplate")
    public String toTypeTemplate(){
        return "/admin/type_template";
    }

    @RequestMapping("listTemplate")
    @ResponseBody
    public Result listTemplate(){
        try {
            List<TbTypeTemplate> list =  typeTemplateService.listTemplate();
            return Result.OK().setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("delTemplates")
    @ResponseBody
    public Result delTemplates(@RequestParam(value = "ids")List<Long> ids){
        try {
            typeTemplateService.delTemplates(ids);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("addTemplate")
    @ResponseBody
    public Result addTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            typeTemplateService.addTemplate(tbTypeTemplate);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.toUpdate(id);
            return Result.OK().setData(tbTypeTemplate);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            typeTemplateService.updateTemplate(tbTypeTemplate);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
}